package list;

/*notes : 1、Add、delete and revise should find the node before the modified
          2、The code is lower , and it is only show the basic linklist structure
*/
public class SingleLinkedListDemo {
    public static void main(String[] args) {
        //测试新建英雄节点
        HeroNode n1 = new HeroNode(1,"宋江", "及时雨");
        HeroNode n2 = new HeroNode(2, "卢俊义", "玉麒麟");
        HeroNode n3 = new HeroNode(10, "吴用", "智多星");
        HeroNode n4 = new HeroNode(4, "林冲", "豹子头");
        SingleLinkedList l1 = new SingleLinkedList();
        //测试尾插英雄节点
        l1.addHero(n1);
        l1.addHero(n2);
        l1.addHero(n4);
        //测试显示英雄信息
        l1.list();
        System.out.println("----------------------------------");
        //测试插入英雄节点
        l1.insertHero(n3);
        //测试根据num修改英雄信息
        //l1.update(n3);
        //测试删除英雄节点
        l1.deleteHero(1);
        l1.deleteHero(4);
        l1.list();
        }
}
//定义SingleLinkedList 管理英雄
class SingleLinkedList {
    private HeroNode head = new HeroNode(0, "", "");

    //①添加英雄节点
    public void addHero(HeroNode heroNode) {
        HeroNode temp = head;
        while (true) {
            if(temp.next == null)
                break;
            temp = temp.next;
        }
        temp.next = heroNode;
    }
    //②插入英雄
    public void insertHero(HeroNode heroNode) {
        HeroNode temp = head;
        if(heroNode.num < 1 )
            return;
        while(temp != null) {
            if(temp.next != null && temp.next.num < heroNode.num)
                temp = temp.next;
            else if (temp.next != null && temp.next.num == heroNode.num) {
                System.out.println("number"+heroNode.num+":repeat!");
                break;
            } else{
                heroNode.next = temp.next;
                temp.next = heroNode;
                break;
            }
        }
    }
    //③根据num修改英雄属性
    public void update(HeroNode heroNode) {
        HeroNode temp = head.next;
        while(temp != null){
            if(heroNode.num == temp.num) {
                temp.name = heroNode.name;
                temp.nickname = heroNode.nickname;
                return;
            }
            else {
                temp = temp.next;
            }
        }
    }
    //④显示链表
    public void list() {
        if(head.next == null) {
            System.out.println("链表为空!");
            return;
        }
        HeroNode temp = head.next;
        while(true) {
            if(temp == null)
                break;
            System.out.println(temp);
            temp = temp.next;
        }


    }
    //⑤删除节点
    public void deleteHero(int num) {
        HeroNode temp = head;
        boolean flag = false;
        while(temp.next != null) {
            if(temp.next.num == num) {
               flag = true;
               break;
            }
            temp = temp.next;
        }
        if(flag) {
            temp.next = temp.next.next;
            return;
        }
        else {
            System.out.println("没有找到待删除节点");
        }
    }
}



class HeroNode{
    public int num;
    public String name;
    public String nickname;
    public HeroNode next;
    //构造器
    public HeroNode(int num, String name, String nickname) {
        this.num = num;
        this.name = name;
        this.nickname = nickname;
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "num=" + num +
                ", name='" + name + '\'' +
                ", nickname='" + nickname + '\'' +
                ", "+ '}';
    }
    }